<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'db_connection.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_date.php';

class dao_briefing_paper {

    //Methods
    public function __construct() {
        
    }

    public function get_all() {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_briefing_papers";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            $item['brief_id'] = $row['brief_id'];
            $item['brief_name'] = $row['brief_name'];
            $item['author'] = $row['author'];
            $item['detail'] = $row['detail'];
            $item['file_upload'] = $row['file_upload'];
            $item['Addedon'] = $row['Addedon'];
            if(!empty($row['Addedon'])) {
                $item['year'] = date("Y", strtotime($row['Addedon']));
            } else {
                $item['year'] = '';
            }
            array_push($list, $item);
        }

        $db->close_connect($con);
        return $list;
    }

    public function add($brief_id, $brief_name, $author, $detail, $file_upload) {
        $db = new db_connection();
        $con = $db->open_connect();

        if ($brief_id == 0) {
            $query = "INSERT INTO tbl_briefing_papers(brief_name, author, detail, file_upload, Addedon) VALUES ('" . $brief_name . "', '" . $author . "','" . $detail . "','" . $file_upload . "', '" . lib_date::get_now() . "')";
        } else {
            $query = "UPDATE tbl_briefing_papers SET brief_name = '" . $brief_name . "' , detail = '" . $detail . "' , file_upload = '" . $file_upload . "', Addedon = '" . lib_date::get_now() . "', author = '" . $author . "' WHERE brief_id = " . $brief_id;
        }
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect($con);
        return true;
    }

    public function get_by_id($brief_id) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_briefing_papers WHERE brief_id = " . $brief_id;
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect($con);
        return $row;
    }

    public function delete($brief_id) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "DELETE FROM tbl_briefing_papers WHERE brief_id = " . $brief_id;
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect($con);
        return true;
    }

    public function get_lastest_ones($number) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_briefing_papers ORDER BY Addedon DESC LIMIT 0, " . $number;
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect($con);
        return $list;
    }

    public function get_paper_by_year ($year) {
        $list_all = $this->get_all();
        $list = array();
        
        foreach ($list_all as $item) {
            if ($item['year'] == $year) {
                array_push($list, $item);
            }
        }
        
        return $list;
    }
}
